package com.tarena.lbs.content.dao.repository.impl;

import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.mapper.CategoryMapper;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
    @Resource
    private CategoryMapper categoryMapper;
    public PageResult<ArticleCategoryVO> pageList() {
        List<ArticleCategoryPO> list = categoryMapper.selectList(null);
        Long count = categoryMapper.selectCount(null);
        List<ArticleCategoryVO> list2 = list.stream().map(this::convertToVO).collect(Collectors.toList());
        PageResult<ArticleCategoryVO> pageResult = new PageResult<>();
        pageResult.setObjects(list2);
        pageResult.setTotal(count);
        pageResult.setPageSize(10);

        return pageResult;
    }

    public void save(ArticleCategoryParam articleCategoryParam) {
        ArticleCategoryPO articleCategoryPO = new ArticleCategoryPO();
        BeanUtils.copyProperties(articleCategoryParam, articleCategoryPO);
        articleCategoryPO.setCreateTime(new Date());
        categoryMapper.insert(articleCategoryPO);
    }

    private ArticleCategoryVO convertToVO(ArticleCategoryPO PO) {
        ArticleCategoryVO VO = new ArticleCategoryVO();
        BeanUtils.copyProperties(PO, VO);
        return VO;
    }
}
